﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LunwenYun.Engine.ExServer.Cache;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace LunwenYun.Portal.Biz
{
    public class iCache
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="loadAction"></param>
        /// <param name="Duration">单位(s)</param>
        /// <param name="immortal">是否永远保存（当因为超时而被移除时，自动重载）</param>
        /// <returns></returns>
        public static T Get<T>(string key, Func<T> loadAction, int duration, bool immortal=false) 
            where T : class
        {
            T data = CacheBag.GetCache<T>(key);
            if (data == null)
            {
                data = loadAction.Invoke();
                if (data != null)
                {
                    return CacheBag.Add<T>(key, data, (k, v, reason) =>
                    {
                        if (reason == CacheItemRemovedReason.Expired)
                        {
                            CacheBag.RemoveCache(key);
                            Get<T>(key, loadAction, duration, true);
                        }
                    }, CacheExpireStrategy.Absolute, new TimeSpan(0, 0, duration));
                }
            }
            return data;
        }
    }
}
